######## Classifier Data Analysis ########
######## Final Year Project 2011#####
######## Making Web Search Spatial###


######## Liam Sullivan       ########
######## University of Leeds ########

import csv
from operator import itemgetter

from decimal import *


from pygooglechart import StackedHorizontalBarChart, StackedVerticalBarChart, \
    GroupedHorizontalBarChart, GroupedVerticalBarChart
    
from pygooglechart import *

TotalDifference = 0

width = 700
height = 400

TopTenTopicHigh = 0
TopicHigh = 0

TopTenTopicLow = 10
TopicLow = 0

DataForAverages = {}


def occursBackwards(a,b, TopicPairs):
    
    #Loop through the topic pairs, can we find a case where B is related to A?
    output = False
    
    #Output flagged to True if we find the pair in question reversed.
    
    for item in TopicPairs:
        
        #If the second topic in the pair is A and the first topic in the pair is B
        if item[1][0] == a and item[0][0] == b:
            
            output = True
            
    return output            
            

def stacked_vertical(TopOne,TopTwo,TopThree,One,Two,Three,FileName):
    chart = StackedVerticalBarChart(width, height,
                                    y_range=(0, 100))
    
    ChartName = "graphs/" + str(FileName) + ".png" 
    
    chart.set_bar_width(150)
    chart.set_colours(['999999', '555555'])
    
    chart.add_data([TopOne,TopTwo,TopThree])
    chart.add_data([One,Two,Three])

    # Some axis data
    chart.set_axis_labels(Axis.LEFT, [0,'Results Hitting Majority (%)', 100])
    chart.set_axis_labels(Axis.BOTTOM, ['Largest Topic', 'Top Two Topics', 'Top Three Topics'])


    chartURL = chart.get_url()
    
    chart.download(ChartName)
    
    return chartURL





#Read in Queries

QueryListFile = open("QueryListFile.txt")

QueryList = []

count = 0

for line in QueryListFile.readlines():
    
    QueryList.append(str(line))
    count += 1
    print "Read query number: " + str(count) + " which is: " + line
    
print "Read in " + str(count) + " queries to pull data for"




AllData = []
    
def getQueryData(query):
    """Return a list of any row in csv that has the request query"""
    
    OutList = []
    
    CSVReader = csv.reader(open('data.csv', 'rb'), delimiter=',', quotechar='"')
    
    for row in CSVReader:
        
        #if this row is of interest
        if row[0] == query:
            ##Add to the output list
            OutList.append(row)
    return OutList

def getClass(index):
    """Maps index column number to class"""

    if index == 4:
        return "Arts"
    if index == 5:
        return "Business"
    if index == 6:
        return "Games"
    if index == 7:
        return "Home"    
    if index == 8:
        return "Health"
    if index == 9:
        return "Society"
    if index == 10:
        return "Sports"
    if index == 11:
        return "Recreation"
    if index == 12:
        return "Computers"
    if index == 13:
        return "Science"
  

outWriter = csv.writer(open('results.csv', 'wb'), delimiter=',', quotechar='"')
    
#Write out headings
outWriter.writerow(["Query", "Most number of Topics", "Least number of topics", "Average number of topics", "Topics Hit", "List of Topics hit in Query"])            
outWriter.writerow(["", "", "", "", "", ])          
#Classifications below this are ignored          
Threshold = 10  


TotalPairCount = 0
TotalTripletsCount = 0
TotalTripletCount = 0

QueryNumber = 0

TopTenTopicDictionary = {}
TopicDictionary = {}

PercentageTopThreeTotal = 0


TopTenTopicDictionary["Arts"] = 0
TopTenTopicDictionary["Business"] = 0
TopTenTopicDictionary["Games"] = 0
TopTenTopicDictionary["Home"] = 0
TopTenTopicDictionary["Health"] = 0
TopTenTopicDictionary["Society"] = 0
TopTenTopicDictionary["Sports"] = 0
TopTenTopicDictionary["Recreation"] = 0
TopTenTopicDictionary["Computers"] = 0
TopTenTopicDictionary["Science"] = 0


TopicDictionary["Arts"] = 0
TopicDictionary["Business"] = 0
TopicDictionary["Games"] = 0
TopicDictionary["Home"] = 0
TopicDictionary["Health"] = 0
TopicDictionary["Society"] = 0
TopicDictionary["Sports"] = 0
TopicDictionary["Recreation"] = 0
TopicDictionary["Computers"] = 0
TopicDictionary["Science"] = 0



    
    
    
           
for query in QueryList:
    
    
    
    # For a given query what is the most popular topic in terms of overall vote from the classifier
    # Now what percentage of all documents (results) fall in this topic?

    TopicTotals = {}
    
    TopicTotals["Arts"] = 0
    TopicTotals["Business"] = 0
    TopicTotals["Games"]= 0
    TopicTotals["Home"]= 0
    TopicTotals["Health"]= 0
    TopicTotals["Society"] = 0  
    TopicTotals["Sports"]= 0
    TopicTotals["Recreation"]  = 0     
    TopicTotals["Computers"] = 0 
    TopicTotals["Science"] = 0  



    
    QueryNumber = QueryNumber + 1
    
    
    
    
    QueryData = getQueryData(query)
    print "Query data for: "
    print query
    print " is below: "
    print QueryData
    
    #Check each classifier field

    ClassList = []   
    
    #How many classes do we consider before we get a majority?
    
    #The number of results that are conclusive after one, increment when we find one.
    ConclusiveAfterOneCount = 0
    #The number of results that are conclusive after two considered topics, increment when we find one.
    ConclusiveAfterTwoCount = 0
    
    ConclusiveAfterThreeCount = 0
    
    
    TOPTENConclusiveAfterOneCount = 0
    #The number of results that are conclusive after two considered topics, increment when we find one.
    TOPTENConclusiveAfterTwoCount = 0
    
    TOPTENConclusiveAfterThreeCount = 0
    
    
    
    #List of topics that are used together to classify results - need to consider these in the layout!!
    TopicSingles = []
    TopicPairs = []
    TopicTriplets = []
    
    
    
    
    
    MostClasses = 0
    LeastClasses = 10
    ResultCount = 0
    TotalNonZeroClassifications = 0
    
    TopicsHitInQuery = []
    ResultPairsInQuery = []
    
    Relevance = 0
    
    TopClasses = []
    
    for result in QueryData:
        
        

        
        PairCount = 0
        
        #A dictionary of integers, class pairs to contain all classification data for this result
        ThisResultClassificationData = {}
        
        
        #These should still be in order of relevance
        Relevance = Relevance + 1
        
        #Add relevance so we can plot it on chart
        
        #A dictionary of integers, class pairs to contain all classification data for this result
       # ThisResultClassificationData['Relevance'] = Relevance
        
        
        ResultCount = ResultCount + 1
        #For each result count how many classifier data there are
        
        ClassCount = 0
     
        
        for i in range(4,14):
            
            #Add each item of classification data to the dictionary
            #Append to the list of classification data for this result
            
            ThisResultClassificationData[getClass(i)] = int(result[i])
            
            #Increment the counter for that is measuring the output across all results
            
            TopicTotals[getClass(i)] = TopicTotals[getClass(i)] + int(result[i])
            

            #Loop through 10 runs to catch all classifier data and add it to the dictionary
            if int(result[i]) > Threshold:
                
                #If this topic is being hit for the first time in this query then add it to the list
                if not getClass(i) in TopicsHitInQuery:
                    TopicsHitInQuery.append(getClass(i))

                
                TotalNonZeroClassifications = TotalNonZeroClassifications + 1
                ClassCount = ClassCount + 1
            
                ClassList.append([getClass(i), int(result[i])])
                
        if ClassCount > MostClasses:
            MostClasses = ClassCount
            
        if ClassCount < LeastClasses:
            LeastClasses = ClassCount 
        
        
        #Relevance is coming through now?
        
            
        #Sort pairs of topic and value by the value    
        DI = ThisResultClassificationData.items()
        
        DI.sort(key=lambda x: x[1])
        
        #Get last index of the list 
            
        #Check the number of conclusive after one topic
        
        # Order Values - these are the class values   
        
        Biggest = DI[9]
        SecondBiggest = DI[8]
        ThirdBiggest = DI[7]
        
        Difference = int(Biggest[1]) - int(SecondBiggest[1])
        
        TotalDifference = TotalDifference + Difference
        
        TopClasses.append(Biggest[0])

        
        if Biggest[1] > 50:
        #We have majority from one single class
            if Relevance < 11:
                #We're in the top 10
                
                #How many times does this happen for this query?
                TOPTENConclusiveAfterOneCount = TOPTENConclusiveAfterOneCount + 1 
                
                #Add topic to top 10 count
                TopTenTopicDictionary[Biggest[0]] = TopTenTopicDictionary[Biggest[0]] + 1     
                
            else:
                
                #Add both topics to the top 10 topic count
                TopicDictionary[Biggest[0]] =  TopicDictionary[Biggest[0]] + 1
                TopicDictionary[SecondBiggest[0]] = TopicDictionary[SecondBiggest[0]] + 1
                
                #Were in the remaining 90 items
                ConclusiveAfterOneCount = ConclusiveAfterOneCount + 1      
            
            TopicSingles.append([DI[9],Relevance])
                   
        
        #Otherwise check out pairs
        elif Biggest[1] + SecondBiggest[1] > 50:
         # We have a majority from two classes
            
            if Relevance < 11:
                #We're in the top 10
                
                #Add both topics to the top 10 topic count
                TopTenTopicDictionary[Biggest[0]] =  TopTenTopicDictionary[Biggest[0]] + 1
                TopTenTopicDictionary[SecondBiggest[0]] = TopTenTopicDictionary[SecondBiggest[0]] + 1 
                
                #How many times does this happen for this query?
                TOPTENConclusiveAfterTwoCount = TOPTENConclusiveAfterTwoCount + 1      
            else:
                
                #Add both topics to the top 10 topic count
                TopicDictionary[Biggest[0]] =  TopicDictionary[Biggest[0]] + 1
                TopicDictionary[SecondBiggest[0]] = TopicDictionary[SecondBiggest[0]] + 1
                
                
                #Were in the remaining 90 items
                ConclusiveAfterTwoCount = ConclusiveAfterTwoCount + 1 
            
            
            TopicPairs.append([DI[9], DI[8], Relevance])
            
            PairCount = PairCount + 1
            
            TotalPairCount = TotalPairCount + 1
            
        elif Biggest[1] + SecondBiggest[1] + ThirdBiggest[1] > 50:
            
            # We have a majority from three classes
            
            if Relevance < 11:
                
                TOPTENConclusiveAfterThreeCount = TOPTENConclusiveAfterThreeCount + 1
                
            else:
                #Were in the remaining 90 items
                #How many times does this happen for this query?
                ConclusiveAfterThreeCount = ConclusiveAfterThreeCount + 1
                
            TopicTriplets.append([DI[9], DI[8], DI[7], Relevance])   
            TotalTripletCount = TotalTripletCount + 1 
            
    AverageDifference = float(0)
    
    AverageDifference = float(TotalDifference) / float(101)
    
    #Average = total classifications over number of results
    
    #average = int(TotalNonZeroClassifications) /  int(ResultCount)   
    
    average = "none"
    
    
    # Query", "Most number of Topics", "Least number of topics", "Average nber of topics"])            
      
    outWriter.writerow([query,MostClasses,LeastClasses, average, len(TopicsHitInQuery), str(TopicsHitInQuery)])


    leftover = len(QueryData) - int(ConclusiveAfterOneCount) - int(ConclusiveAfterTwoCount) - int(ConclusiveAfterThreeCount) 
    
    
    
    TopicTotalList = TopicTotals.items()
    TopicTotalList.sort(key=lambda x: x[1])
    
    MostPopularTopic = TopicTotalList[9]
    SecondMostPopularTopic = TopicTotalList[8]
    ThirdMostPopularTopic = TopicTotalList[7] 
    FourthMostPopularTopic = TopicTotalList[6] 

    
   
    outWriter.writerow(["Most voted for topic across all results for this query:" ,str(MostPopularTopic), float(float(MostPopularTopic[1]) / float(10000)*100), "% of total classification"])
    outWriter.writerow(["Second most voted for topic across all results for this query:" ,str(SecondMostPopularTopic),  float(float(SecondMostPopularTopic[1]) / float(10000)*100), "% of total classification"])
    outWriter.writerow(["Third most voted for topic across all results for this query:" ,str(ThirdMostPopularTopic),  float(float(ThirdMostPopularTopic[1]) / float(10000)*100), "% of total classification"])
    outWriter.writerow(["Fourth most voted for topic across all results for this query:" ,str(FourthMostPopularTopic),  float(float(FourthMostPopularTopic[1]) / float(10000)*100), "% of total classification"])
    
    print "Most voted for topic across all results for this query:" ,str(MostPopularTopic), float(float(MostPopularTopic[1]) / float(10000)*100), "% of total classification"
    print "Second most voted for topic across all results for this query:" ,str(SecondMostPopularTopic),  float(float(SecondMostPopularTopic[1]) / float(10000)*100), "% of total classification"
    print "Third most voted for topic across all results for this query:" ,str(ThirdMostPopularTopic),  float(float(ThirdMostPopularTopic[1]) / float(10000)*100), "% of total classification"
    
    #What percentage of results for this query have topic votes forming conclusivity (>50%) in one of these top three classes?
    TopThree = {}
    
    TopThree[MostPopularTopic[0]] = MostPopularTopic[0] 
    TopThree[SecondMostPopularTopic[0]] = SecondMostPopularTopic[0]
    TopThree[ThirdMostPopularTopic[0]] = ThirdMostPopularTopic[0]

    
    ###answer this question and stick to thought process in LYX!!
    
    
    TotalConclusiveClassifiedResults = 0
    
    x = len(TopicSingles)
    y = len(TopicPairs)
    z = len(TopicTriplets)
    
    
    TotalConclusiveClassifiedResults = x + y + z
    
    
    
    NumberOfResultsInTop3Topics = 0
    
    #loop through all 3 lists of conclusive classification, in each one count the number of sets containing one of our top 3
            
        
    print "Number of results who's conclisive classification set contains one of the top three"
    
    print "Number of results who's conclisive classification set contains one of the top three"
    
    TopThreeCounter = 0
    
    for item in TopClasses:
        if item in TopThree:
            TopThreeCounter = TopThreeCounter + 1
            
    PercentageTopThree = float(TopThreeCounter) / float(len(TopClasses)) * 100
            
    print "Percentage of results whos top class is in the top 3 over all based on classifier output"
    
    outWriter.writerow(["Percentage of results whos top class is in the top 3 over all based on classifier output", PercentageTopThree])
    
    #Add this to average across all queries
    
    PercentageTopThreeTotal = PercentageTopThreeTotal + PercentageTopThree
    
    print "We have " + str(ConclusiveAfterOneCount) + " results that are easy to categorise, showing a majority with one vote"
    outWriter.writerow(["We have ", str(ConclusiveAfterOneCount), "results that are easy to categorise, showing a majority with one vote", ])
       
    
    print "We have: " + str(ConclusiveAfterTwoCount) + " results that take two topic votes to reach a majority"    
    outWriter.writerow(["We have ", str(ConclusiveAfterTwoCount), "results that take two topic votes to reach a majority", ])
    
   
    print "The following are topics pairs that classify a result together: "   
    
    
    outWriter.writerow(["The following rows present pairs of topics that together classify results: "])
    
    outWriter.writerow(["", "", "", "", "", ])
    
    outWriter.writerow(["Topic A", "Topic A Value", "Topic B", "Topic B Value",  "Relevance"])
    
    for item in TopicPairs:
        print str(item[0]) + " AND " + str(item[1])
        
        outWriter.writerow([item[0][0], item [0][1], item[1][0], item[1][1], Relevance])
        
    outWriter.writerow(["", "", "", "", "", ])
    outWriter.writerow(["Here is the set of 'problem pairs' this time listed only once each: "])
    
    UniqueTopicPairs = TopicPairs   
    
    for item in UniqueTopicPairs:
        #For each pair in the list    
         
        if occursBackwards(item[0][0], item[1][0], UniqueTopicPairs):
            #Occursbackwards removes it's copies but we still use it here
            
            print "This pair occurs backwards print it out but remove the backwards element"
            
            outWriter.writerow([item[0][0], item [0][1], item[1][0], item[1][1], Relevance])
            
            
        else:
            print "this pair doesn't occur backwards"
            #Only print and add to output file if they don't occur backwards!
            print str(item[0]) + " AND " + str(item[1])
            
            outWriter.writerow([item[0][0], item [0][1], item[1][0], item[1][1], Relevance])
        
        
    outWriter.writerow(["", "", "", "", "", ])        
    
    
    print "The following are topic triplets that classify a result together: "    
    
    outWriter.writerow(["The following rows present triplets of 3 topics that together classify results: "])
    
    outWriter.writerow(["", "", "", "", "", ])
    
    outWriter.writerow(["Topic A", "Topic A Value", "Topic B", "Topic B Value","Topic C", "Topic C Value" , "Relevance"])
    
    if TopicTriplets:
        for item in TopicTriplets:
            
            print str(item[0]) + " , " + str(item[1]) + " AND" + str(item[2])
            outWriter.writerow([item[0][0], item [0][1], item[1][0], item[1][1],item[2][0], item[2][1], Relevance])
        
    print "So for this result there are roughly: " + str(leftover) + " left over that don't give us a majority after top 3 classes considered"
    
    outWriter.writerow(["Leftover: ", str(leftover)])
    
    outWriter.writerow(["", "", "", "", "", ])
    
    outWriter.writerow(["~~~~~~", "~~~~~~", "~~~~~~", "~~~~~~", "~~~~~~", ])
    
    outWriter.writerow(["", "", "", "", "", ])
    
    TOPTENPercentageHittingMajorityAfterOneTopic = float(TOPTENConclusiveAfterOneCount) / float(ResultCount)
    TOPTENPercentageHittingMajorityAfterOneTopic = float(TOPTENPercentageHittingMajorityAfterOneTopic) * 100
    
    TOPTENPercentageHittingMajorityAfterTwoTopic = float(TOPTENConclusiveAfterTwoCount) / float(ResultCount)
    TOPTENPercentageHittingMajorityAfterTwoTopic = float(TOPTENPercentageHittingMajorityAfterTwoTopic) * 100
    TOPTENPercentageHittingMajorityAfterTwoTopic = TOPTENPercentageHittingMajorityAfterTwoTopic + TOPTENPercentageHittingMajorityAfterOneTopic
    
    TOPTENPercentageHittingMajorityAfterThreeTopic = float(TOPTENConclusiveAfterThreeCount) / float(ResultCount)
    TOPTENPercentageHittingMajorityAfterThreeTopic = float(TOPTENPercentageHittingMajorityAfterThreeTopic) * 100
    TOPTENPercentageHittingMajorityAfterThreeTopic = TOPTENPercentageHittingMajorityAfterThreeTopic + TOPTENPercentageHittingMajorityAfterTwoTopic
    
    
    
    PercentageHittingMajorityAfterOneTopic = float(ConclusiveAfterOneCount) / float(ResultCount)
    PercentageHittingMajorityAfterOneTopic = float(PercentageHittingMajorityAfterOneTopic) * 100
    
    PercentageHittingMajorityAfterTwoTopic = float(ConclusiveAfterTwoCount) / float(ResultCount)
    PercentageHittingMajorityAfterTwoTopic = float(PercentageHittingMajorityAfterTwoTopic) * 100
    PercentageHittingMajorityAfterTwoTopic = PercentageHittingMajorityAfterTwoTopic + PercentageHittingMajorityAfterOneTopic
    
    PercentageHittingMajorityAfterThreeTopic = float(ConclusiveAfterThreeCount) / float(ResultCount)
    PercentageHittingMajorityAfterThreeTopic = float(PercentageHittingMajorityAfterThreeTopic) * 100
    PercentageHittingMajorityAfterThreeTopic = PercentageHittingMajorityAfterThreeTopic + PercentageHittingMajorityAfterTwoTopic    
    
    
    TOPTENPercentageHittingMajorityAfterTwoTopic = float(TOPTENConclusiveAfterTwoCount)
    
    #Loop through all keys in dictionry
    
    
    
    TopTenTopics = []
    Bottom90Topics = []
    
    
    
    
    for key, value in TopTenTopicDictionary.iteritems():
        
        if value > 0:
            #This class popped up in a conclusive classification over 50% single or as a pair breaking the 50 barrier
            TopTenTopics.append(key)
            
            
    for key, value in TopicDictionary.iteritems():
        
        if value > 0:
            
            Bottom90Topics.append(key)
    
    
    #Consider spread in two groups, top ten and bottom 90
    

    
    outWriter.writerow(["Topics Hit in the Top Ten: ", TopTenTopics])
    
    #If this topic spread is higher than the highest update
    if len(TopTenTopics) > TopTenTopicHigh:
        TopTenTopicHigh = len(TopTenTopics)
        
    #If this topic spread is lower than the lowest update
    if len(TopTenTopics) < TopTenTopicLow:
        TopTenTopicLow = len(TopTenTopics)

    outWriter.writerow(["Count: ", len(TopTenTopics)])
    

    
    
    print "Topics Hit in the bottom 90:  " +  str(Bottom90Topics)
            
    #If this topic spread is higher than the highest update
    if len(Bottom90Topics) > TopicHigh:
        TopicHigh = len(Bottom90Topics)
        
    #If this topic spread is lower than the lowest update
    if len(Bottom90Topics) < TopicLow:
        TopicLow = len(Bottom90Topics)
    
    outWriter.writerow(["Topics Hit in the bottom 90:  ", Bottom90Topics])
    outWriter.writerow(["Count: ", len(Bottom90Topics)])
    

    
    
    
    
    outWriter.writerow(["Spread for Top Ten Results"])
    
    TOPTENAfterOne = float(TOPTENConclusiveAfterOneCount) / float(10.00)
    
    TOPTENAfterOne = TOPTENAfterOne * 100
    
    outWriter.writerow(["In this query:",TOPTENAfterOne , "% of the top ten results require just ONE topic", " to get conclusivity"])
    
    
    TOPTENAfterTwo = float(TOPTENConclusiveAfterTwoCount) / float(10.00)
    
    TOPTENAfterTwo = TOPTENAfterTwo * 100
    
    outWriter.writerow(["In this query:",TOPTENAfterTwo , "% of the top ten results require TWO topics", " to get conclusivity"])
    
    
    TOPTENAfterThree = float(TOPTENConclusiveAfterThreeCount) / float(10.00)
    
    TOPTENAfterThree = TOPTENAfterThree * 100
    
    outWriter.writerow(["In this query:",TOPTENAfterThree , "% of the top ten % of  results require THREE topics", " to get conclusivity"])
    
      
    #Consider spread in two groups, BOTTOM 90% of  and bottom 90
    outWriter.writerow(["Spread for Bottom 90 Results"])
    
    
    AfterOne = float(ConclusiveAfterOneCount) / float(ResultCount)
    
    AfterOne = AfterOne * 100
    
    outWriter.writerow(["In this query:",AfterOne , "% of the BOTTOM 90% of  results require just ONE topic", " to get conclusivity"])
    
    
    AfterTwo = float(ConclusiveAfterTwoCount) / float(ResultCount)
    
    AfterTwo = AfterTwo * 100
    
    outWriter.writerow(["In this query:",AfterTwo , "% of the BOTTOM 90% of  results require TWO topics", " to get conclusivity"])
    
    
    AfterThree = float(ConclusiveAfterThreeCount) / float(ResultCount)
    
    AfterThree = AfterThree * 100
    
    outWriter.writerow(["In this query:",AfterThree , "% of the BOTTOM 90% of  results require THREE topics", " to get conclusivity"])
       
    
    #Build chart for each query here?
    
    stacked_vertical(TOPTENPercentageHittingMajorityAfterOneTopic,TOPTENPercentageHittingMajorityAfterTwoTopic,TOPTENPercentageHittingMajorityAfterThreeTopic, PercentageHittingMajorityAfterOneTopic,PercentageHittingMajorityAfterTwoTopic,PercentageHittingMajorityAfterThreeTopic,QueryNumber)

outWriter.writerow(["Total Number of pairs flagged up", "i.e. total number of pairs need to consider careful placement for layout", TotalPairCount ])
outWriter.writerow(["Total Number of triplets flagged up", "i.e. total number of triplet sets need to consider careful placement for layout", TotalTripletCount ])   

AveragePercentageTopThreeTotal = float(PercentageTopThreeTotal) / float(101)

print "Percentage of results in the top three topics for the average query", AveragePercentageTopThreeTotal

outWriter.writerow(["Percentage of results in the top three topics for the average query", AveragePercentageTopThreeTotal])
 


AverageTopTenTopicSpread = float(TopTenTopicSpreadTotal) / float(101)

AverageTopicSpread = float(TopicSpreadTotal) / float(101)

outWriter.writerow(["Average Topic Spread in the top 10 results across all queries: ",  AverageTopTenTopicSpread ])   

outWriter.writerow(["Average Topic Spread in the bottom 90 results across all queries: ",  AverageTopicSpread ])  



outWriter.writerow(["Topic Spread Range Info:"])

outWriter.writerow(["Lowest Topic Spread for Top 10's across all queries: ", TopTenTopicLow])  

outWriter.writerow(["Highest Topic Spread for Top 10's across all queries: ", TopTenTopicHigh]) 



outWriter.writerow(["lowest Topic Spread for Bottom 90% of results across all queries: ", TopicLow]) 

outWriter.writerow(["Highest Topic Spread for Bottom 90% of results across all queries: ", TopicHigh]) 

#What is the most popular category?

print "What percentage of results reside in the most popular category?"


print "Done!"    
    
    

    
    
    
            
    
    

    
    

            
    

